package com.kgc.kmall.itemweb.controller;

import com.kgc.kmall.bean.PmsProductSaleAttr;
import com.kgc.kmall.bean.PmsSkuInfo;
import com.kgc.kmall.bean.PmsSkuSaleAttrValue;
import com.kgc.kmall.service.SkuService;
import com.kgc.kmall.service.SpuService;
import org.apache.dubbo.common.json.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ItemController {

    @Reference
    SkuService skuService;

    @Reference
    SpuService spuService;

    @RequestMapping("{skuId}.html")
    public String item(@PathVariable Long skuId, Model model){
        PmsSkuInfo skuInfo=skuService.selectBySkuId(skuId);
        model.addAttribute("skuInfo",skuInfo);
        //根据spuid获取销售属性和属性值
        List<PmsProductSaleAttr> saleAttrList = spuService.spuImageListBySpuIdAndSkuId(skuInfo.getSpuId(),skuInfo.getId());
        model.addAttribute("spuSaleAttrListCheckBySku",saleAttrList);


        List<PmsSkuInfo> pmsSkuInfos = skuService.seleByViews(skuInfo.getSpuId());
        Map<String, String> skuSaleAttrHash = new HashMap<>();
        for (PmsSkuInfo skuInfos : pmsSkuInfos) {
            String k = "";
            String v=skuInfos.getId()+"";
            for (PmsSkuSaleAttrValue pmsSkuSaleAttrValue : skuInfos.getSkuSaleAttrValueList()) {
                k+=pmsSkuSaleAttrValue.getSaleAttrValueId()+"|";

            }
            skuSaleAttrHash.put(k.substring(0,k.length()-1),v);
        }

        System.out.println(skuSaleAttrHash.toString());
        // 将sku的销售属性hash表放到页面
        String skuSaleAttrHashJsonStr =com.alibaba.fastjson.JSON.toJSONString(skuSaleAttrHash);
        model.addAttribute("skuSaleAttrHashJsonStr",skuSaleAttrHashJsonStr);
        return "item";
    }



}
